package handlers

import (
	"net/http"
	"encoding/json"
	"io"
	"log"
	"io/ioutil"
)

//SendJsonResp 发送json应答
func SendJsonResp(w http.ResponseWriter,r *response)error{
	jsonBytes,err:=json.Marshal(r)
	if err!=nil{
		w.WriteHeader(http.StatusInternalServerError)
		return err
	}
	w.Header().Set("content-type","application/json")
	io.WriteString(w,string(jsonBytes))
	return nil
}

//SendServerErrResp 发送服务内部错误应答
func SendServerErrResp(w http.ResponseWriter)error{
	return SendJsonResp(w,BuildServerErrResp())
}


//SendStaticHTML 发送静态html内容
func SendStaticHTML(path string,w http.ResponseWriter,r *http.Request) error{
	htmlData, err := ioutil.ReadFile(path)
	if err != nil {
		log.Printf("SendStaticHTML内部错误:%v", err.Error())
		SendServerErrResp(w)
		return err
	}
	log.Printf("SendStaticHTML获取到HTML，字节数：%v", len(htmlData))
	io.WriteString(w, string(htmlData))
	return nil
}